package net.starwifi.duobao.web.db;

import net.starwifi.duobao.FileInfo;
import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Iterator;

/**
 * Created by Administrator on 2015/12/23.
 */
@Controller("apiDBMemberShareController")
@RequestMapping("/member")
public class DBMemberShareController extends BaseController {

    @Autowired
    DBOrderService dbOrderService;

    @Autowired
    DBShareService dbShareService;

    @Autowired
    DBShareImageService dbShareImageService;

    @Autowired
    FileService fileService;

    @Autowired
    MemberService memberService;

    @Autowired
    DBTimesService dbTimesService;

    @Autowired
    DBProductService dbProductService;

    /**
     * 我的中奖详情信息
     */
    @RequestMapping(value = "/dbShare-tip-{id}", method = RequestMethod.GET)
    public String dbShareTip(Model model,@PathVariable Long id) {
        model.addAttribute("id",id);
        return "/db/share-tip";
    }


    /**
     * 我的中奖详情信息
     */
    @RequestMapping(value = "/dbShare-edit-{id}", method = RequestMethod.GET)
    public String dbShareEdit(Model model,@PathVariable Long id) {
        model.addAttribute("id",id);
        return "/db/share-edit";
    }


    /**
     * 我的中奖详情信息
     */
    @RequestMapping(value = "/dbShare-save", method = RequestMethod.POST)
    public String dbShareEditPost(DBShare dbShare,Model model,Long orderId) {
        Member member=memberService.getCurrent();
        DBOrder dbOrder=dbOrderService.find(orderId);
        if(!dbOrder.getMemberId().equals(member.getId())){
            model.addAttribute("content","对不起,请求非法");
            return "/db/error";
        }
        if(!dbOrder.getIsLottery()){
            model.addAttribute("content","对不起,您不是中奖用户");
            return "/db/error";
        }
        if(dbOrder.getIsShare()!=null){
            model.addAttribute("content","对不起,已经晒过单");
            return "/db/error";
        }
        //DBTimes dbTimes=dbTimesService.find(dbOrder.getDbTimesId());
        //dbShare.setDbTimes(dbTimes);
        dbShare.setDbTimes(dbOrder.getDbTimes());
        int i=0;
        for (Iterator<DBShareImage> iterator = dbShare.getDbShareImages().iterator(); iterator.hasNext();) {
            DBShareImage shareImage = iterator.next();
            if (shareImage == null || shareImage.isEmpty()) {
                iterator.remove();
                continue;
            }
            if (shareImage.getFile() != null && !shareImage.getFile().isEmpty()) {
                if (!fileService.isValid(FileInfo.FileType.image, shareImage.getFile())) {
                    model.addAttribute("content","图片格式有误");
                    return "/db/error";
                }
            }
            i=i+1;
        }
        if(i<3){
            model.addAttribute("content","最少上传3张图片");
            return "/db/error";
        }
        if (!isValid(dbShare)) {
            model.addAttribute("content","图片格式有误");
            return "/db/error";
        }
        for (DBShareImage shareImage : dbShare.getDbShareImages()) {
            dbShareImageService.build(shareImage,"/db_share_"+dbShare.getDbTimes().getTimesNo()+"/");
        }
        DBProduct dbProduct=dbProductService.find(dbOrder.getProductId());
        dbShare.setProductId(dbProduct.getId());
        dbShare.setProductName(dbProduct.getName());
        dbShare.setCount(dbOrder.getQuantity());
        dbShare.setMember(member);
        dbShare.setIsShow(Boolean.FALSE);
        dbShareService.save(dbShare);
        dbOrder.setIsShare(Boolean.TRUE);
        dbOrderService.update(dbOrder); //将订单改为已经晒单
        return "redirect:/member/dbRecord/winDetail-"+dbOrder.getId();
    }
}
